package com.mcc.app.carbonEmission.task;

import com.mcc.app.carbonEmission.domain.ReportEquipInfo;
import com.mcc.app.carbonEmission.service.IReportAssistHanService;
import com.mcc.app.carbonEmission.service.IReportAssistService;
import com.mcc.app.carbonEmission.service.IReportEquipInfoService;
import com.mcc.app.carbonEmission.service.IReportProductDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class carbonTask   implements CommandLineRunner {

    @Autowired
    private IReportEquipInfoService reportEquipInfoService;

    @Autowired
    private IReportProductDataService reportProductDataService;

    @Autowired
    private IReportAssistService reportAssistService;

    @Autowired
    private IReportAssistHanService reportAssistHanService;
    @Override
    public void run(String... args) {

       //工序生产设施信息表
        generateReportEquip();
        //工序生产数据及排放量表
        generateReportProductData();

        //辅助报表
//        generateReportAssist();
    }


    @Scheduled(cron = "1 0 0 * * ?")
    private void generateReportEquip(){
        //（以2025.05.13算至今多少天）,计算投运时间
//        Date baseDate = new Date(2025, 5, 13);
        LocalDate endDate = LocalDate.now(); // 当前日期
        LocalDate startDate = LocalDate.of(2025, 5, 13); // 目标日期

        long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
        ReportEquipInfo reportEquipInfo  = new ReportEquipInfo();
        List<ReportEquipInfo> resultList= reportEquipInfoService.selectReportEquipInfoList(reportEquipInfo);

        if(null != resultList && resultList.size() != 0){
            ReportEquipInfo result =resultList.get(0);
            result.setEquip1Runtime((double)daysBetween);
            reportEquipInfoService.updateReportEquipInfo(result);
            System.out.println("天数: " + daysBetween);
        }

        //本月正常生产 天，停产 天，检修 天，如果有点位则自动记算，但目前没有点位，所以目前就手动输入

    }

    @Scheduled(cron = "5 0 0 * * ?")
    private void generateReportProductData(){
        //定时刷新测量值
        reportProductDataService.generateProductData(true);
    }

    @Scheduled(cron = "5 0 0 * * ?")
    private void generateReportAssist(){
        //定时刷新测量值
        reportAssistService.generateAssistData(true);
    }
}
